<template>
  <div class="app-container">

    <el-row :gutter="20">
      <!--部门数据-->
      <el-col :span="4" :xs="24">
        <div class="head-container">
          <el-input
            v-model="deptName"
            placeholder="请输入部门名称"
            clearable
            size="small"
            prefix-icon="el-icon-search"
            style="margin-bottom: 20px"
          />
        </div>
        <div class="head-container">
          <el-tree
            :data="deptOptions"
            :props="defaultProps"
            :expand-on-click-node="false"
            :filter-node-method="filterNode"
            ref="tree"
            node-key="id"
            default-expand-all
            highlight-current
            @node-click="handleNodeClick"
          />
        </div>
      </el-col>
      <!--用户数据-->
      <el-col :span="20" :xs="24">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
                 label-width="68px">
          <el-form-item label="姓名" prop="psonName">
            <el-input
              v-model="queryParams.psonName"
              placeholder="请输入姓名"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="性别" prop="psonSex">
            <el-select v-model="queryParams.psonSex" placeholder="请选择性别" clearable>
              <el-option
                v-for="dict in dict.type.sys_user_sex"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
<!--          <el-form-item label="相关部门" prop="deptId">-->
<!--            <el-input-->
<!--              v-model="queryParams.deptId"-->
<!--              placeholder="请输入相关部门"-->
<!--              clearable-->
<!--              @keyup.enter.native="handleQuery"-->
<!--            />-->
<!--          </el-form-item>-->
<!--          <el-form-item label="所属关系" prop="gxDeptId">-->
<!--            <el-input-->
<!--              v-model="queryParams.gxDeptId"-->
<!--              placeholder="请输入所属关系"-->
<!--              clearable-->
<!--              @keyup.enter.native="handleQuery"-->
<!--            />-->
<!--          </el-form-item>-->
          <el-form-item label="出生日期" prop="dateBirth">
            <el-date-picker clearable
                            v-model="queryParams.dateBirth"
                            type="date"
                            value-format="yyyy-MM-dd"
                            placeholder="请选择出生日期">
            </el-date-picker>
          </el-form-item>
          <el-form-item label="联系电话" prop="psonPhone">
            <el-input
              v-model="queryParams.psonPhone"
              placeholder="请输入联系电话"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="邮箱" prop="psonEmail">
            <el-input
              v-model="queryParams.psonEmail"
              placeholder="请输入邮箱"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="地址" prop="psonAddress">
            <el-input
              v-model="queryParams.psonAddress"
              placeholder="请输入地址"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="primary"
              plain
              icon="el-icon-plus"
              size="mini"
              @click="handleAdd"
              v-hasPermi="['system:pson:add']"
            >新增
            </el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="success"
              plain
              icon="el-icon-edit"
              size="mini"
              :disabled="single"
              @click="handleUpdate"
              v-hasPermi="['system:pson:edit']"
            >修改
            </el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="danger"
              plain
              icon="el-icon-delete"
              size="mini"
              :disabled="multiple"
              @click="handleDelete"
              v-hasPermi="['system:pson:remove']"
            >删除
            </el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="warning"
              plain
              icon="el-icon-download"
              size="mini"
              @click="handleExport"
              v-hasPermi="['system:pson:export']"
            >导出
            </el-button>
          </el-col>
          <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="psonList" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55" align="center"/>
          <el-table-column label="人脉id" align="center" prop="psonId"/>
          <el-table-column label="姓名" align="center" prop="psonName"/>
          <el-table-column label="性别" align="center" prop="psonSex">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.sys_user_sex" :value="scope.row.psonSex"/>
            </template>
          </el-table-column>
          <!--      <el-table-column label="相关部门" align="center" prop="deptId" />-->
          <!--      -->
          <!--      -->
          <!--      <el-table-column label="所属关系" align="center" prop="gxDeptId" />-->
          <el-table-column label="所属关系" align="center" prop="gxName"/>
          <el-table-column label="出生日期" align="center" prop="dateBirth" width="180">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.dateBirth, '{y}-{m}-{d}') }}</span>
            </template>
          </el-table-column>
          <el-table-column label="照片url" align="center" prop="photoUrl" width="100">
            <template slot-scope="scope">
              <image-preview :src="scope.row.photoUrl" :width="50" :height="50"/>
            </template>
          </el-table-column>
          <!--      <el-table-column label="文件url" align="center" prop="fileUrl" />-->
          <el-table-column label="文件url" align="center" prop="fileUrl">
            <template slot-scope="scope">
              <a :href="'http://localhost:8080'+scope.row.fileUrl">
                <el-button type="success">下载文件</el-button>
              </a>
            </template>
          </el-table-column>
          <el-table-column label="联系电话" align="center" prop="psonPhone"/>
          <el-table-column label="邮箱" align="center" prop="psonEmail"/>
          <el-table-column label="地址" align="center" prop="psonAddress"/>

          <el-table-column label="详情" align="center" prop="psonInfo">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-show"
                @click="psonInfoShow(scope.row)"
              >查看详情
              </el-button>
            </template>
          </el-table-column>

          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
                v-hasPermi="['system:pson:edit']"
              >修改
              </el-button>
              <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
                v-hasPermi="['system:pson:remove']"
              >删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </el-col>
    </el-row>
    <!-- 添加或修改人脉管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="姓名" prop="psonName">
          <el-input v-model="form.psonName" placeholder="请输入姓名"/>
        </el-form-item>
        <el-form-item label="性别" prop="psonSex">
          <el-radio-group v-model="form.psonSex">
            <el-radio
              v-for="dict in dict.type.sys_user_sex"
              :key="dict.value"
              :label="dict.value"
            >{{ dict.label }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="相关部门" prop="deptId">
          <treeselect v-model="form.deptId" :options="psDeptOptions" :show-count="true" placeholder="请选择归属部门"/>
        </el-form-item>
        <el-form-item label="所属关系" prop="gxDeptId">
          <treeselect v-model="form.gxDeptId"  :options="gxDeptOptions" :show-count="true" multiple placeholder="请输入所属关系"/>
        </el-form-item>

        <el-form-item label="出生日期" prop="dateBirth">
          <el-date-picker clearable
                          v-model="form.dateBirth"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择出生日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="照片url" prop="photoUrl">
          <image-upload v-model="form.photoUrl"/>
        </el-form-item>
        <el-form-item label="文件url" prop="fileUrl">
          <file-upload v-model="form.fileUrl"/>
        </el-form-item>
        <el-form-item label="联系电话" prop="psonPhone">
          <el-input v-model="form.psonPhone" placeholder="请输入联系电话"/>
        </el-form-item>
        <el-form-item label="邮箱" prop="psonEmail">
          <el-input v-model="form.psonEmail" placeholder="请输入邮箱"/>
        </el-form-item>
        <el-form-item label="地址" prop="psonAddress">
          <el-input v-model="form.psonAddress" placeholder="请输入地址"/>
        </el-form-item>
        <el-form-item label="详情">
          <editor v-model="form.psonInfo" :min-height="192"/>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 详情 -->
    <el-dialog title="详情" :visible.sync="openPsonInfo" width="800px" append-to-body>
      <span v-html="setPsonInfo"></span>
      <div slot="footer" class="dialog-footer">
        <el-button @click="psonInfoCancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listPson, getPson, delPson, addPson, updatePson} from "@/api/system/pson";
import Treeselect from "@riophae/vue-treeselect";
import {deptTreeSelect} from "@/api/system/user";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "Pson",
  components: {Treeselect},
  dicts: ['sys_user_sex'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 人脉管理表格数据
      psonList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      //开关
      openPsonInfo: false,
      // 搜索时
      deptOptions: undefined,
      //添加时
      psDeptOptions: undefined,
      //关系树状图
      gxDeptOptions: undefined,
      setPsonInfo: null,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        psonName: null,
        psonSex: null,
        deptId: null,
        gxDeptId: null,
        psonInfo: null,
        dateBirth: null,
        photoUrl: null,
        fileUrl: null,
        psonPhone: null,
        psonEmail: null,
        psonAddress: null,
      },
      // 表单参数
      form: {},
      defaultProps: {
        children: "children",
        label: "label"
      },
      // 表单校验
      rules: {
        psonName: [
          { required: true, message: "姓名不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
    this.getDeptTree();
  },
  methods: {

    /** 查询部门下拉树结构 */
    getDeptTree() {
      deptTreeSelect().then(response => {
        this.deptOptions = response.data;
        this.psDeptOptions = [response.data[0]];
        this.gxDeptOptions = [response.data[1]];
      });
    },
    /** 查询人脉管理列表 */
    getList() {
      this.loading = true;
      listPson(this.queryParams).then(response => {
        this.psonList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 取消按钮
    psonInfoCancel() {
      this.openPsonInfo = false;
      this.setPsonInfo = null;
    },
    // 表单重置
    reset() {
      this.form = {
        psonId: null,
        psonName: null,
        psonSex: null,
        deptId: null,
        gxDeptId: null,
        psonInfo: null,
        dateBirth: null,
        photoUrl: null,
        fileUrl: null,
        psonPhone: null,
        psonEmail: null,
        psonAddress: null,
        createTime: null,
        updateTime: null,
        createBy: null,
        updateBy: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.psonId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加人脉管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const psonId = row.psonId || this.ids



      getPson(psonId).then(response => {
        this.form = response.data;
        this.form.gxDeptId = this.form.gxDeptId.split(",").map(Number);
        this.open = true;
        this.title = "修改人脉管理";
      });
    },
    psonInfoShow(row) {
      this.setPsonInfo = row.psonInfo;
      this.openPsonInfo = true;

    },
    // 筛选节点
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    // 节点单击事件
    handleNodeClick(data) {


      this.queryParams.deptId = data.id;
      this.handleQuery();
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {

          this.form.gxDeptId = this.form.gxDeptId.join(",");
          if (this.form.psonId != null) {
            updatePson(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPson(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const psonIds = row.psonId || this.ids;
      this.$modal.confirm('是否确认删除人脉管理编号为"' + psonIds + '"的数据项？').then(function () {
        return delPson(psonIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/pson/export', {
        ...this.queryParams
      }, `pson_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
